<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**  Training Page **/
class Training extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model("trainings");
		$this->load->helper('text');
	}
	
	public function index($categories='')
	{
		$value['title'] = "Training";
		$value['cat_link'] = $categories;
		if($categories)
			$value['cat_title'] = $this->trainings->getTitleCategories($categories);
		else
			$value['cat_title'] = 'New Training';
        $value['categories'] = $this->trainings->getTrainingCategories();
		$value['records'] = $this->trainings->getAllRecords($categories);
        $value['content'] = "vtraining/vindex";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function read($year,$month,$day,$cat,$id){
		
		$value['title']	  = "Training";	
		$value['cat_link'] = $cat;
		if($cat)
			$value['cat_title'] = $this->trainings->getTitleCategories($cat);
		else
			$value['cat_title'] = 'New Training';
		$value['post']	  = $this->trainings->getDataSingleRecord($id,$cat,'',$year,$month,$day);	
		$value['categories'] = $this->trainings->getTrainingCategories();
		$value['meta']	  = $value['post']['training_title'];
		$value['files']   = $this->trainings->getAllFileRecords($id);	
		$value['content'] = "vtraining/vread-training";
		$this->load->view('themes/bdo/index',$value);
	}
    
    public function add(){
		$value['title']	  = "Training";
		$value['cat']	  = $this->trainings->getDropdownCategories();
		$value['categories'] = $this->trainings->getTrainingCategories();
		$value['content'] = "vtraining/vadd";
		$this->load->view('themes/bdo/index',$value);
	}
    
    public function save(){
		$this->trainings->saveTraining();
		redirect('training',301);
	}
    
    public function edit($id){
		$value['title']	  = "Training";
		$value['cat']	  = $this->trainings->getDropdownCategories();
        $value['categories'] = $this->trainings->getTrainingCategories();
		$value['post']	  = $this->trainings->getDataSingleRecord($id);
		$value['content'] = "vtraining/vedit";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function update(){
        $this->trainings->updateTraining();
		redirect('training/edit/'.$this->input->post('id'),301);
	}
	
	public function uploadFile(){
		$config['upload_path'] = './assets/files/';
		$config['allowed_types'] = 'pdf|xls|xlsx|doc|docx';
		$config['max_size']	   = '0';
		$config['max_width']   = '0';
		$config['max_height']  = '0';
		$config['file_name']   = date('Y-m-d').'-'.seoUrl($this->input->post('title'));
		$this->load->library('upload',$config);
		if (!$this->upload->do_upload('file')):
			$data = '';
		else:
			$data = $this->upload->data();
		endif;
		
		if(!empty($data))
			$file = $data['file_name'];
		else
			$file = '';
		return $file;
	}
	
	public function saveFile(){
		$file = $this->uploadFile(); // call file
		$this->trainings->saveFile($file);
		if($file)
			$this->trainings->updateMaterial();
		redirect($this->input->server('HTTP_REFERER'),301);
	}
    
    public function remove($id){
		$this->trainings->removeTraining($id);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	public function removeFile($id){
		$this->trainings->removeFile($id);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	public function add_category(){
		$value['title']	  = "Training";
		$value['cat']	  = $this->trainings->getDropdownCategories();
		$value['categories'] = $this->trainings->getTrainingCategories();
		$value['content'] = "vtraining/vcategory";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function saveCategory(){
		$this->trainings->saveCategory();
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	public function removeCategory($id){
		$this->trainings->removeCategory($id);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
}

/* End of file training.php */
/* Location: ./application/controllers/training.php */